package com.eshop.modules.monitor.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import com.eshop.modules.aop.ForbidSubmit;
import org.springframework.web.bind.annotation.RequestBody;
import com.eshop.modules.monitor.domain.vo.RedisVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import com.eshop.modules.monitor.service.RedisService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "redis缓存管理" })
@RestController
@RequestMapping({ "api" })
public class RedisController
{
    @Autowired
    private RedisService redisService;
    
    @Log("查询Redis缓存")
    @GetMapping({ "/redis" })
    @PreAuthorize("hasAnyRole('ADMIN','REDIS_ALL','REDIS_SELECT')")
    public ResponseEntity getRedis(final String key, final Pageable pageable) {
        return new ResponseEntity(this.redisService.findByKey(key, pageable), HttpStatus.OK);
    }
    
    @ForbidSubmit
    @Log("删除Redis缓存")
    @DeleteMapping({ "/redis" })
    @PreAuthorize("hasAnyRole('ADMIN','REDIS_ALL','REDIS_DELETE')")
    public ResponseEntity delete(@RequestBody final RedisVo resources) {
        this.redisService.delete(resources.getKey());
        return new ResponseEntity(HttpStatus.OK);
    }
    
    @ForbidSubmit
    @Log("清空Redis缓存")
    @DeleteMapping({ "/redis/all" })
    @PreAuthorize("hasAnyRole('ADMIN','REDIS_ALL','REDIS_DELETE')")
    public ResponseEntity deleteAll() {
        this.redisService.flushdb();
        return new ResponseEntity(HttpStatus.OK);
    }
}
